
setwd("c:/users/james/desktop/dropbox/transparency_and_democracy/transparencyindex/PAReplicationMaterials/PostPred Checks/")


library(arm)
library(foreign)
library(rjags)
library(R2jags)

load("TwoDimenIRT.RData")

attach.jags(results)
TePCP2Dim<-cbind(1:80)

for (j in 1:240){
  eval(parse(text=sprintf("p.hat%s <- cbind(1:3875)", j)))
  eval(parse(text=sprintf("xb%s <- cbind(1:3875)",j)))
  eval(parse(text=sprintf("diff%s <- cbind(1:3875)", j)))
}

for (t in 29:80){
  k <- 0
  for (c in 1:125){
    for (y in 1:31){
      k <- k+1
      for (j in 1:240){
        eval(parse(text=sprintf("xb%s[k] <- beta%s[%s,1] + beta%s[%s,2]*transparency[%s,%s,%s]+beta%s[%s,3]*trans2[%s,%s,%s]",
        j, j, t,j, t,t,c,y,j,t,t,c,y)))
        eval(parse(text=sprintf("p.hat%s[k] <- 1/(1+exp(-xb%s[k]))", j,j)))
      }
    }
  }
  TePCP2Dim[t] <- 0
  for (j in 1:240){
    eval(parse(text=sprintf("diff%s <- 1 - abs(WDIdata$var_%s - p.hat%s)",j,j,j)))
    eval(parse(text=sprintf("TePCP2Dim[t] <- TePCP2Dim[t] + mean(diff%s)", j)))
  }
}

TePCP2Dim <- TePCP2Dim/240
